package com.zhangkeyou.map.mapweb.domain.entity;

import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.zhangkeyou.map.mapweb.mapper.handler.FeatureFieldsTypeHandler;
import com.zhangkeyou.map.mapweb.mapper.handler.FeaturesTypeHandler;
import lombok.Data;

import java.util.List;
import java.util.Map;
import java.util.UUID;

@Data
@TableName(value = "features",autoResultMap = true)
public class Features {

    @TableField(value = "id")
    @JSONField(name = "id")
    private String id;

    @TableField("level")
    private int level;
    @TableField("features_name")
    private String featuresName;

    @JSONField(name = "geometryType")
    @TableField("geometryType")
    private String geometryType;

    @JSONField(name="fields")
    @TableField(value = "fields",typeHandler = FeatureFieldsTypeHandler.class)
    private List<Map<String,String>> fields;

    @JSONField(name="features")
    @TableField(value = "features",typeHandler = FeaturesTypeHandler.class)
    private List<Feature> features;

    public void setFields(List<Map<String, String>> fields) {
        this.fields = fields;
    }

    public Features(){
        this.id= UUID.randomUUID().toString();
    }



    public void setFeatures(List<Feature> featureList){
/*        if(featureList.size()!=0){
            this.level = (int) featureList.get(0).getAttributes().get("level");
            this.featuresName = (String) featureList.get(0).getAttributes().get("fsname");
        }*/
        featureList.forEach(feature -> {
            feature.setFeaturesId(this.id);
            if(feature.getFeatureId()==null)
                feature.setFeatureId(UUID.randomUUID().toString());
        });
        this.features=featureList;
    }

    /**
     * 从属性表中提取feature的属性
     * @param featureIdFieldName featureID字段名
     * @param featureParentIdFieldName feature的父对象ID字段名
     * @param featureNameFieldName featureName的字段名
     */
    public void extractFeatureFromAttribute(String featureIdFieldName ,String featureParentIdFieldName,String featureNameFieldName){
        this.features.forEach(feature -> {
            Map<String, Object> attributes = feature.getAttributes();
            String featureName = attributes.get(featureNameFieldName).toString();
            feature.setName(featureName);
            String featureId =  attributes.get(featureIdFieldName).toString();
            feature.setId(featureId);
            if(level>1) {
                String parentId =  attributes.get(featureParentIdFieldName).toString();
                feature.setParentId(parentId);
            }
        });
    }

    @Override
    public String toString() {
        return "Features{" +
                "id='" + id + '\'' +
                ", featuresName='" + featuresName + '\'' +
                ", features=" + features +
                '}';
    }

 /*    @Override
    public String toString() {
        return "Features{" +
                "id='" + id + '\'' +
                ", level=" + level +
                ", featuresName='" + featuresName + '\'' +
                ", features.size=" + features.size() +
                '}';
    }*/
}